#include "StringInput.h"
#include <iostream>

StringInput::StringInput(string contenido) {
    str = contenido;
    text = NULL;
    SDL_EnableUNICODE( SDL_ENABLE );
}

StringInput::~StringInput() {
    SDL_FreeSurface( text );
    SDL_EnableUNICODE( SDL_DISABLE );
}

string StringInput::getString() {
	return str;
}

SDL_Surface* StringInput::getText() {
	return text;
}

void StringInput::setString(string contenido) {
	str = contenido;
}

void StringInput::handle_input(SDL_Event event, TTF_Font* font, unsigned int id) {

	SDL_Color black =  {0, 0, 0};
    if( event.type == SDL_KEYDOWN )
    {

        string temp = str;


        if( event.key.keysym.unicode == (Uint16)'.' ||  event.key.keysym.unicode == (Uint16)'_' ||
        	event.key.keysym.unicode == (Uint16)'/' ||  event.key.keysym.unicode == (Uint16)'-' ||
        	event.key.keysym.unicode == (Uint16)'#' ||  event.key.keysym.unicode == (Uint16)'$' ||
        	event.key.keysym.unicode == (Uint16)'%' ||  event.key.keysym.unicode == (Uint16)'(' ||
        	event.key.keysym.unicode == (Uint16)')' ||  event.key.keysym.unicode == (Uint16)'=' ||
        	event.key.keysym.unicode == (Uint16)'"' ||  event.key.keysym.unicode == (Uint16)' ' ||
        	event.key.keysym.unicode == (Uint16)'!' ||  event.key.keysym.unicode == (Uint16)'?' ||
        	event.key.keysym.unicode == (Uint16)'+' ||  event.key.keysym.unicode == (Uint16)'*' ||
        	event.key.keysym.unicode == (Uint16)'|' ||  event.key.keysym.unicode == (Uint16) '<'||
        	event.key.keysym.unicode == (Uint16)'{' ||  event.key.keysym.unicode == (Uint16)'}' ||
        	event.key.keysym.unicode == (Uint16)'[' ||  event.key.keysym.unicode == (Uint16)']' ||
        	event.key.keysym.unicode == (Uint16)'&' ||  event.key.keysym.unicode == (Uint16)'~' ||
        	event.key.keysym.unicode == (Uint16)'^' ||  event.key.keysym.unicode == (Uint16)';' ||
        	event.key.keysym.unicode == (Uint16)':' ||  event.key.keysym.unicode == (Uint16)',' ||
        	event.key.keysym.unicode == (Uint16)'>' ||  event.key.keysym.unicode == (Uint16)'@'  )
            {

                str += (char)event.key.keysym.unicode;
            }

            else if( ( event.key.keysym.unicode >= (Uint16)'0' ) && ( event.key.keysym.unicode <= (Uint16)'9' ) )
            {

                str += (char)event.key.keysym.unicode;
            }

            else if( ( event.key.keysym.unicode >= (Uint16)'A' ) && ( event.key.keysym.unicode <= (Uint16)'Z' ) )
            {
                str += (char)event.key.keysym.unicode;
            }

            else if( ( event.key.keysym.unicode >= (Uint16)'a' ) && ( event.key.keysym.unicode <= (Uint16)'z' ) )
            {

                str += (char)event.key.keysym.unicode;
            }
            else if ( ( event.key.keysym.sym == SDLK_BACKSPACE ) && ( str.length() != 0 ) )
			{
				
				str.erase( str.length() - 1  );
			}
        }

            SDL_FreeSurface( text );

            text = TTF_RenderText_Solid( font, str.c_str(), black );

}
